<?php
/*
---------------------------------------------------------------------------
install.php for SALAM Frontend

Copyright 2009, 2010 Jacob McEntire

    This file is part of SALAM.

    SALAM is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SALAM is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SALAM.  If not, see <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
*/
include('../library/settings.php');

echo '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Install SALAM</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="icon" type="image/png" href="images/icon.png" />
<link rel="shortcut icon" href="/favicon.ico">
<link rel="stylesheet" href="style.css" />
</head>
<div class="header">
<div class="logo">
<h1>SALAM</h1>
</div>
</div>
<div class="frame">
<div class="body">';

if (isset($_POST['time_zone'])) {
	$settings_data = "<?php\n";
	$settings_data .= '$time_zone = "' . $_POST['time_zone'] . '";';
	$settings_data .= '$from_email = "' . $_POST['from_email'] . '";';
	$settings_data .= '$to_email = "' . $_POST['to_email'] . '";';
	$settings_data .= '$dbhost = "' . $_POST['dbhost'] . '";';
	$settings_data .= '$dbname = "' . $_POST['dbname'] . '";';
	$settings_data .= '$dbuser = "' . $_POST['dbuser'] . '";';
	$settings_data .= '$dbpass = "' . $_POST['dbpass'] . '";';
	$settings_data .= '$keep_data = "' . $_POST['keep_data'] . '";';
	$settings_data .= '$keep_ratio = "' . $_POST['keep_ratio'] . '";';
	$settings_data .= '$refresh_frequency = "' . $_POST['refresh_frequency'] . '";';
	$settings_data .= '$install_dir = "' . $_POST['install_dir'] . '";';
	$settings_data .= '$email_start = "' . $_POST['email_start'] . '";';
	$settings_data .= '$email_end = "' . $_POST['email_end'] . '";';
	$settings_data .= '$wmi_user = "' . $_POST['wmi_user'] . '";';
	$settings_data .= '$wmi_password = "' . $_POST['wmi_password'] . '";';
	$settings_file = "../library/settings.php";
	if (is_writable($settings_file)){
		if (!$handle = fopen($settings_file, 'w'))
			echo 'Cannot open file ', $settings_file;
		elseif (fwrite($handle, $settings_data) === FALSE)
			echo 'Cannot write to file ', $settings_file;
		else {
			echo '<form method="post" action="install.php" enctype="multipart/form-data">
			<table width="500" border="0" cellpadding="1" cellspacing="1"><tr><th colspan=2>SALAM Administration Password:</th></tr>
			<tr><td colspan="2" align="center">Password for SALAM Administration Mode, username is "admin"</td></tr>
			<tr><td align="right">Password:</td><td><input type="password" name="password" /></td></tr>
			<tr><td align="right">Confirm:</td><td><input type="password" name="confirm" /></td></tr>
			<tr><td colspan="2" align="center"><input name="next" type="submit" class="box" value=" Next "></td></tr>
			</table>
			</form>';
		}
		fclose($handle);
	}
	else
		echo $settings_file, ' is not writeable';
}
elseif (isset($_POST['password'])) {
	if ($_POST['password'] == $_POST['confirm']) {
		$pass = crypt(trim($_POST['password']),base64_encode(CRYPT_STD_DES));
		$file = "admin/.htpasswd";
		$data = 'admin:' . $pass;
		if (is_writable($file)){
			if (!$handle = fopen($file, 'w'))
				echo 'Cannot open file ', $file;
			elseif (fwrite($handle, $data) === FALSE)
				echo 'Cannot write to file ', $htpasswd_file;
			else {
				echo 'Password Saved Successfully!<br /><a href="cleanup.php">Finish</a>';
			}
			fclose($handle);
		}
		else
			echo $file, ' is not writeable';
	}
	else {
		echo '<h3>Passwords Do Not Match</h3>
			<form method="post" action="install.php" enctype="multipart/form-data">
			<table width="500" border="0" cellpadding="1" cellspacing="1"><tr><th colspan=2>SALAM Administration Password:</th></tr>
			<tr><td colspan="2" align="center">Password for SALAM Administration Mode, username is "admin"</td></tr>
			<tr><td align="right">Password:</td><td><input type="password" name="password" /></td></tr>
			<tr><td align="right">Confirm:</td><td><input type="password" name="confirm" /></td></tr>
			<tr><td colspan="2" align="center"><input name="next" type="submit" class="box" value=" Next "></td></tr>
			</table>
			</form>';
	}
}
else {
	require("../library/timezones.php");
	echo '<form method="post" action="install.php" enctype="multipart/form-data">
	<table width="500" border="0" cellpadding="1" cellspacing="1">
	<tr><th colspan=2>Time:</th></tr>
	<tr><td align="right">Time Zone:</td>
	<td><select name="time_zone">';
	foreach ($tz as $value => $name) {
		echo '<option value="', $value, '"';
		if ($value == $time_zone)
			echo ' selected="selected"';
		echo '>', $name, '</option>';
	}
	echo '</select></td></tr>
	<tr><th colspan=2>Email Settings:</th></tr>
	<tr><td align="right">From Email Address:</td><td><input type="text" size="35" name="from_email" value="', $from_email, '" /></td></tr>
	<tr><td align="right">To Email Address:</td><td><input type="text" size="35" name="to_email" value="', $to_email, '" /> Separate multiple address with a comma</td></tr>
	<tr><td aligh="right">Only Email New Alert Notifications between:</td><td><select name="email_start">';
	for ($i = 0; $i <= 24; $i++) {
		echo '<option value="', $i, '"';
		if ($i == $email_start)
			echo ' selected="selected"';
		echo '>', $i, ':00</option>';
	}
	echo '</select> And <select name="email_end">';
	for ($i = 0; $i <= 24; $i++) {
		echo '<option value="', $i, '"';
		if ($i == $email_end)
			echo ' selected="selected"';
		echo '>', $i, ':00</option>';
	}
	echo '</select> (Changes to existing alerts will still be emailed)</td></tr>
	<tr><th colspan=2>MySQL Settings:</th></tr>
	<tr><td align="right">MySQL Host:</td><td><input type="text" name="dbhost" value="', $dbhost, '" /> - Should be "localhost" for most installs</td></tr>
	<tr><td align="right">DB Name:</td><td><input type="text" name="dbname" value="', $dbname, '" /> - Should be "salam" for most installs</td></tr>
	<tr><td align="right">DB User:</td><td><input type="text" name="dbuser" value="', $dbuser, '" /> - Should be "root" for most installs</td></tr>
	<tr><td align="right">DB Password:</td><td><input type="password" name="dbpass" value="', $dbpass, '" /> - Asked during MySQL setup(or if using Live CD leave blank)</td></tr>
	<tr><th colspan=2>Database Maintenance:</th></tr>
	<tr><td align="right">Clean up and Average Data Older Than:</td><td><input type="text" size="3" name="keep_data" value="', $keep_data, '" /> Days</td></tr>
	<tr><td align="right">Average Ratio:</td><td><input type="text" size="3" name="keep_ratio" value="', $keep_ratio, '" /> Records will be averaged down to 1</td></tr>
	<tr><th colspan=2>Microsoft WMI Settings:</th></tr>
	<tr><td align="right">WMI Username(domain/username):</td><td><input type="text" name="wmi_user" value="', $wmi_user, '" /></td></tr>
	<tr><td align="right">WMI Password:</td><td><input type="password" name="wmi_password" value="', $wmi_password, '" /></td></tr>
	<tr><th colspan=2>Misc:</th></tr>
	<tr><td align="right">AJAX Auto Refresh Frequency:</td><td><input type="text" size="3" name="refresh_frequency" value="', $refresh_frequency, '" /> Seconds</td></tr>
	<tr><td align="right">SALAM Install Location:</td><td><input type="text" name="install_dir" value="', $install_dir, '" /> - Should be "/usr/local/salam/" for most installs</td></tr>
	<tr><td colspan="2" align="center"><input name="next" type="submit" class="box" value=" Next "></td></tr>
	</table>
	</form>';
}



echo '
</div>
</div>
<div class="footer">
SALAM v 0.1 RC1<br />
Current Date/Time: ', date("D M j G:i:s T Y"), '
</div>

</body>
</html>';
?>